package com.smedi.ismedi.organization.core.port.adapter.restful;

import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.organization.api.dto.request.*;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.SendMessageByJobRespose;
import com.smedi.ismedi.organization.api.dto.response.SendMessageByRoleRespose;
import com.smedi.ismedi.organization.api.dto.response.resp.AllOrganizationResp;
import com.smedi.ismedi.organization.api.dto.response.resp.OrganizationResp;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.organization.core.application.command.*;
import com.smedi.ismedi.organization.core.application.query.HrOrganizationQueryService;
import com.smedi.ismedi.organization.core.application.query.vo.AllOrganizationVo;
import com.smedi.ismedi.organization.core.application.query.vo.HrOrganizationVo;
import com.smedi.ismedi.organization.core.application.service.HrOrgManageApplicationService;
import com.smedi.ismedi.organization.core.domain.aggregatemodel.HrOrganization;
import com.smedi.ismedi.organization.core.domain.aggregatemodel.model.Organization;
import com.smedi.ismedi.organization.core.domain.aggregatemodel.SendMessage;
import com.smedi.ismedi.organization.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@AllArgsConstructor
public class HrOrganizationManageController implements RemoteHrOrganizationFeignApi {
    private final DozerUtil dozerUtil;

    private final AdapterConverter adapterConverter;

    private final HrOrgManageApplicationService hrOrgManageApplicationService;

    private final HrOrganizationQueryService hrOrganizationQueryService;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<HrOrganizationResponse> postSaveHrOrganization(@RequestBody HrOrganizationRequest hrOrganizationRequest) {
        // 请求对象转为入参对象
        HrOrganizationCommand hrOrganizationCommand = dozerUtil.convertor(hrOrganizationRequest, HrOrganizationCommand.class);
        // 返回请求接口数据
        return new ResponseData<HrOrganizationResponse>().success(dozerUtil.convertor(hrOrgManageApplicationService.saveHrOrganization(hrOrganizationCommand), HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteHrOrganization(Long hrOrganizationId) {
        hrOrgManageApplicationService.deleteHrOrganization(hrOrganizationId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getHrOrganizationTree(Long hrOrganizationId) {
        List<HrOrganizationResponse> list = dozerUtil.convertor(hrOrganizationQueryService.getHrOrganizationTree(hrOrganizationId), HrOrganizationResponse.class);
        return new ResponseData<List<HrOrganizationResponse>>().success(list);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SkipSqlPermission
    public ResponseData<List<HrOrganizationResponse>> getHrOrganizationTreeNoSecurity(Long hrOrganizationId) {
        return getHrOrganizationTree(hrOrganizationId);
    }

    @Override
    public ResponseData<Long> updateByName(HrOrgUpdateRequest orgUpdateRequest) {
        HrOrgUpdateCommand command =dozerUtil.convertor(orgUpdateRequest,HrOrgUpdateCommand.class);
        return new ResponseData<Long>().success(hrOrgManageApplicationService.updateHrOrgByShortName(command));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<HrOrganizationResponse> getHrOrganizationView(Long hrOrganizationId) {
        // 返回请求接口数据
        return new ResponseData<HrOrganizationResponse>().success(dozerUtil.convertor(hrOrganizationQueryService.getHrOrganizationView(hrOrganizationId), HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getCheckAvailable(Long hrOrganizationId) {
        return new ResponseData<Boolean>().success(hrOrgManageApplicationService.checkHrOrganizationIsAvailable(hrOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<Long>> getHrOrganizationChildrenOrgIdList(Long hrOrganizationId) {
        return new ResponseData<List<Long>>().success(hrOrganizationQueryService.getHrOrganizationChildrenIdList(hrOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Map<Long, String>> getHrOrganizationChildrenOrgMap(Long hrOrganizationId) {
        return new ResponseData<Map<Long, String>>().success(hrOrganizationQueryService.getHrOrganizationChildrenMap(hrOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getCheckHrOrganizationCanHasProjectAvailable(Long hrOrganizationId) {
        return new ResponseData<Boolean>().success(hrOrgManageApplicationService.checkHrOrganizationCanHasProjectAvailable(hrOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryHrOrganizationList(List<Long> hrOrganizationIds) {
        List<HrOrganizationVo> list = hrOrganizationQueryService.getHrOrganizationByHrOrganizationIds(hrOrganizationIds);
        List<HrOrganizationResponse> responses = dozerUtil.convertor(list, HrOrganizationResponse.class);

        return new ResponseData<List<HrOrganizationResponse>>().success(responses);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryHrOrganizationListByFullName(String fullName) {
        List<HrOrganizationVo> list = hrOrganizationQueryService.queryHrOrganizationListByFullName(fullName);
        return new ResponseData<List<HrOrganizationResponse>>().success(dozerUtil.convertor(list, HrOrganizationResponse.class));
    }

    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryHrOrganizationListByFullNameList(List<String> fullNameList) {
        List<HrOrganizationVo> list = hrOrganizationQueryService.queryHrOrganizationListByFullNameList(fullNameList);
        return new ResponseData<List<HrOrganizationResponse>>().success(dozerUtil.convertor(list, HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<HrOrganizationResponse> getHrOrganizationRoot() {
        HrOrganizationResponse response = dozerUtil.convertor(hrOrganizationQueryService.getHrOrganizationViewRoot(), HrOrganizationResponse.class);
        return new ResponseData<HrOrganizationResponse>().success(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryAllProjectManagementHROrganizationList() {
        return new ResponseData<List<HrOrganizationResponse>>().success(dozerUtil.convertor(hrOrganizationQueryService.queryAllProjectManagementHROrganizationList(), HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryAllProjectManagementHROrganizationListNew() {
        return new ResponseData<List<HrOrganizationResponse>>().success(dozerUtil.convertor(hrOrganizationQueryService.queryAllProjectManagementHROrganizationListNew(), HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> getQueryAllHROrganizationListByType(boolean isCompany, boolean isCompanyLeader, boolean isCompanyManagementOrganization, boolean isProjectManagementOrganization, boolean isSubordinateOrganization) {
        return new ResponseData<List<HrOrganizationResponse>>().success(dozerUtil.convertor(hrOrganizationQueryService.queryAllHROrganizationListByType(isCompany, isCompanyLeader, isCompanyManagementOrganization, isProjectManagementOrganization, isSubordinateOrganization), HrOrganizationResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<HrOrganizationResponse> getTopHrOrganizationScopeList(List<Long> assignmentHrOrganizationIds, boolean showAll) {
        HrOrganizationResponse response = dozerUtil.convertor(hrOrganizationQueryService.queryAllTopScopeHrOrganizations(assignmentHrOrganizationIds, showAll), HrOrganizationResponse.class);
        return new ResponseData<HrOrganizationResponse>().success(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<Long>> getParentHrOrganizationIdsByHrOrganizationId(Long hrOrganizationId) {
        return new ResponseData<List<Long>>().success(hrOrganizationQueryService.getParentHrOrganizationIdsByHrOrganizationId(hrOrganizationId));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<Long>> getAllHrOrganizationIdsWhenYouAreInCompanyLevel(List<Long> internalAssignmentHrOrganizationIds) {
        return new ResponseData<List<Long>>().success(hrOrganizationQueryService.getAllHrOrganizationIdsWhenYouAreInCompanyLevel(internalAssignmentHrOrganizationIds));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<HrOrganizationResponse>> postExchangeTwoHrOrganizationNodeSort(TwoHrOrganizationNodeRequest twoHrOrganizationNodeRequest) {
        hrOrgManageApplicationService.exchangeTwoHrOrganizationNodeSort(dozerUtil.convertor(twoHrOrganizationNodeRequest.getHrOrganizationPartOne(), HrOrganizationCommand.class),
                dozerUtil.convertor(twoHrOrganizationNodeRequest.getHrOrganizationPartTwo(), HrOrganizationCommand.class));

        return this.getHrOrganizationTree(twoHrOrganizationNodeRequest.getHrOrganizationId());
    }

    @Override
    public ResponseData<List<HrOrganizationResponse>> getAllHrOrganizationList() {
        List<HrOrganizationVo> hrOrganizationVos = hrOrganizationQueryService.getAllHrOrganizationList();
        List<HrOrganizationResponse> hrOrganizationResponseList = dozerUtil.convertor(hrOrganizationVos, HrOrganizationResponse.class);
        return new ResponseData<>().success(hrOrganizationResponseList);
    }

    @Override
    public ResponseData<List<HrOrganizationResponse>> getHrOrganizationList(List<Long> organizationIds) {
        List<HrOrganization> hrOrganizations = hrOrganizationQueryService.getByHrOrganizationIds(organizationIds);

        return new ResponseData<>().success(dozerUtil.convertor(hrOrganizations, HrOrganizationResponse.class));
    }

    @Override
    public ResponseData<OrganizationResp> getOrganizationOne(OrganizationOneQueryRequest organizationOneQueryRequest) {
        OrganizationOneQueryCommand organizationOneQueryCommand = adapterConverter.organizationOneQueryRequestToOrganizationOneQueryCommand(organizationOneQueryRequest);
        Organization organization = hrOrganizationQueryService.getOrganizationOne(organizationOneQueryCommand);
        OrganizationResp organizationResp = adapterConverter.organizationsToOrganizationResp(organization);
        return new ResponseData<>().success(organizationResp);
    }

    @Override
    public ResponseData<List<OrganizationResp>> getOrganizationList(OrganizationListQueryRequest organizationListQueryRequest) {
        OrganizationListQueryCommand organizationListQueryCommand = adapterConverter.organizationListQueryRequestToOrganizationListQueryCommand(organizationListQueryRequest);
        List<Organization> organizations = hrOrganizationQueryService.getOrganizationList(organizationListQueryCommand);
        List<OrganizationResp> organizationResps = adapterConverter.organizationsToOrganizationResp(organizations);
        return new ResponseData<>().success(organizationResps);
    }

    @Override
    public ResponseData<List<OrganizationResp>> getOrganizationListWithoutToken(OrganizationListQueryRequest organizationListQueryRequest) {
        OrganizationListQueryCommand organizationListQueryCommand = adapterConverter.organizationListQueryRequestToOrganizationListQueryCommand(organizationListQueryRequest);
        List<Organization> organizations = hrOrganizationQueryService.getOrganizationListWithoutToken(organizationListQueryCommand);
        List<OrganizationResp> organizationResps = adapterConverter.organizationsToOrganizationResp(organizations);
        return new ResponseData<>().success(organizationResps);
    }

    @Override
    public ResponseData<List<AllOrganizationResp>> getOrgResponseData(List<Long> ids) {
        List<AllOrganizationVo> allOrganizationVoList = hrOrganizationQueryService.getAllOrganizationVos(ids);
        List<AllOrganizationResp> resps =adapterConverter.allOrganizationVoToAllOrganizationResp(allOrganizationVoList);
        return new ResponseData<>().success(resps);
    }

    @Override
    public ResponseData<SendMessageByRoleRespose> sendMessageByRoleNotExists(SendMessageByRoleRequest sendMessageByRoleRequest) {
        SendMessageByRoleCommand sendMessageByRoleCommand = adapterConverter.sendMessageByRoleRequestToSendMessageByRoleCommand(sendMessageByRoleRequest);
        SendMessage sendMessage = hrOrgManageApplicationService.sendMessageByRoleNotExists(sendMessageByRoleCommand);
        return new ResponseData<Boolean>().success(adapterConverter.sendMessagesToSendMessageByRoleRespose(sendMessage));
    }

    @Override
    public ResponseData<List<SendMessageByJobRespose>> sendMessageByJobNotExists(SendMessageByJobRequest sendMessageByJobRequest) {
        SendMessageByJobCommand sendMessageByJobCommand = adapterConverter.sendMessageByJobRequestToSendMessageByJobCommand(sendMessageByJobRequest);
        List<SendMessage> sendMessages = hrOrgManageApplicationService.sendMessageByJobNotExists(sendMessageByJobCommand);
        return new ResponseData<Boolean>().success(adapterConverter.sendMessagesToSendMessageByJobRespose(sendMessages));
    }
}
